"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var TypeHelper = require("./TypeHelper");
var ExceptionHelper = require("./ExceptionHelper");
var JsonHelper = require("./JsonHelper");
function numberCast(option, defaultValue) {
    if (option === undefined)
        return defaultValue;
    if (typeof option === "number")
        return option;
    if (/^(([1-9][0-9]*(\.\d{1,2})?)|(0\.\d{1,2}))$/.test(option)) {
        return Number(option);
    }
    return ExceptionHelper.assertNumber(option);
}
exports.numberCast = numberCast;
function stringCast(option) {
    if (!TypeHelper.isStringWithNotEmpty(option))
        return ExceptionHelper.assertString(option);
    return option;
}
exports.stringCast = stringCast;
function booleanCast(option, defaultValue) {
    if (option === undefined)
        return defaultValue;
    if (typeof option !== "boolean")
        ExceptionHelper.assertBoolean(option);
    return option;
}
exports.booleanCast = booleanCast;
function stringOrBooleanStyleCast(option, defaultValue, trueValue) {
    if (option === undefined)
        return defaultValue;
    if (option === true) {
        if (trueValue === undefined) {
            return defaultValue;
        }
        else {
            return trueValue;
        }
    }
    if (option === false)
        return null;
    return option;
}
exports.stringOrBooleanStyleCast = stringOrBooleanStyleCast;
function typeOrBooleanCast(option, defaultValue, trueValue, falseValue) {
    if (option === undefined)
        return defaultValue;
    if (option === true) {
        if (trueValue === undefined) {
            return defaultValue;
        }
        else {
            return trueValue;
        }
    }
    if (option === false) {
        if (falseValue === undefined) {
            return false;
        }
        else {
            return falseValue;
        }
    }
    return option;
}
exports.typeOrBooleanCast = typeOrBooleanCast;
function undefinedCast(option, defaultValue) {
    if (option === undefined)
        return defaultValue;
    else
        return option;
}
exports.undefinedCast = undefinedCast;
function stringOrUndefinedCast(option) {
    if (option === undefined)
        return undefined;
    if (!TypeHelper.isStringWithNotEmpty(option))
        return ExceptionHelper.assertString(option);
    return option;
}
exports.stringOrUndefinedCast = stringOrUndefinedCast;
function jsonOrBooleanCast(option, defaultValue, trueValue, falseValue) {
    if (option === undefined)
        return defaultValue;
    if (option === true) {
        if (trueValue === undefined) {
            return defaultValue;
        }
        else {
            return trueValue;
        }
    }
    if (option === false) {
        if (falseValue === undefined) {
            return false;
        }
        else {
            return falseValue;
        }
    }
    if (defaultValue === false)
        return option;
    return JsonHelper.merge(defaultValue, option);
}
exports.jsonOrBooleanCast = jsonOrBooleanCast;
function stringOrElementCast(option) {
    if (!TypeHelper.isStringOrElement(option))
        return ExceptionHelper.assertNever(option);
    return option;
}
exports.stringOrElementCast = stringOrElementCast;
function tupleStringOrElementCast(option) {
    if (!(option instanceof Array)
        || option.length !== 2
        || !TypeHelper.isStringOrElement(option[0])
        || !TypeHelper.isStringOrElement(option[1]))
        return ExceptionHelper.assertNever(option);
    return option;
}
exports.tupleStringOrElementCast = tupleStringOrElementCast;
